﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace szse
{
    class MySpider
    {
        public string Domain = "http://http://www.szse.cn";
        public string WebSite = "http://www.szse.cn/main/disclosure/news/tfpts/";
        public string StartDate = "2011-06-23";
        public string EndDate = "2011-06-27";
        public int PageNo = 1;

        public List<Notice> CollectNotice()
        {
            List<Notice> result = new List<Notice>();
            List<Notice> lastlist = new List<Notice>();

            bool equal = false;
            this.PageNo = 0;
            while (!equal)
            {
                this.PageNo++;
                //Console.WriteLine("PageNo={0}", this.PageNo.ToString());
                string content = this.SendPostRequest();
                List<Notice> list = this.ParseWebPage(content, this.Domain);
                //Console.WriteLine(list.Count);
                if (list.Count == lastlist.Count)
                {
                    bool e = true;
                    for (int i = 0; i < lastlist.Count; i++)
                    {
                        if (!lastlist[i].Equals(list[i]))
                        {
                            e = false;
                            break;
                        }
                    }
                    equal = e;
                }
                if (!equal)
                {
                    lastlist = list;
                    result.AddRange(list);
                }
            }

            return result;
        }

        public string SendPostRequest()
        {
            Encoding myEncoding = Encoding.GetEncoding("GBK");
            string postData = "txtDmorjc=";   //code11, date01, date11
            postData += string.Format("&txtKsrq={0}", StartDate);
            postData += string.Format("&txtZzrq={0}", EndDate);
            postData += ("&txtIgnore=");


            byte[] data = myEncoding.GetBytes(postData);

            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(this.WebSite);
            myRequest.Method = "POST";
            myRequest.ContentType = "application/x-www-form-urlencoded;charset=gb2312";
            myRequest.ContentLength = data.Length;
            //myRequest.UserAgent = 

            using (Stream newStream = myRequest.GetRequestStream())
            {
                newStream.Write(data, 0, data.Length);
            }

            string content = "";
            using (HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse())
            {
                StreamReader reader = new StreamReader(myResponse.GetResponseStream(), myEncoding);
                content = reader.ReadToEnd();
                //Console.WriteLine(content);
            }

            return content;
        }


        public List<Notice> ParseWebPage(string content, string domain)
        {
            List<Notice> result = new List<Notice>();

            string startTag = "class='cls-data-tr-head'";
            string endTag = "id='tbl-data-bottom-line'";
            string flagTag = "class='cls-data-tr'";
            string notflagTag = "没有找到符合条件的数据";
            int start = content.IndexOf(startTag);
            int end = content.IndexOf(endTag);
            if (start < 0 || end < 0 || start >= end)
            {
                //报错
                //throw new Exception("页面格式找不到开头或结尾标记");
                return result;
            }

            content = content.Substring(start, end - start);
            //Console.WriteLine(content);
            string[] lines = content.Split(new string[] { "/tr><tr" }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string line in lines)
            {
                //Console.WriteLine(line + "\n");
                if (line.IndexOf(flagTag) >= 0 && line.IndexOf(notflagTag) < 0)
                {
                    Notice n = new Notice();
                    n.ParseFromLine(line, domain);
                    result.Add(n);
                }
            }

            return result;
        }
    }
}
